﻿using AutoMapper;
using ManagementApi.Authorizers;
using ManagementApi.Models;
using ManagementApi.Models.Device;
using ManagementApi.Mvc;
using ManagementApi.Mvc.ApplicationContexts;
using ManagementApi.Mvc.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.Corp;
using Modules.Device;
using Modules.Device.Entitys;
using PmSoft.Utilities;

namespace ManagementApi.Controllers.AdminPannel
{
    /// <summary>
    /// 身份证阅读设备相关接口
    /// </summary>
    [Route("/api/v1/sysuser/cardReaderDevice")]
    [ApiController]
    [Authorize(Roles = "SysUser")]
    public class CardReaderDeviceController : ControllerBase
    {
        private readonly CardReaderDeviceService cardReaderDeviceService;
        private readonly ManagementApplicationContext applicationContext;
        private readonly CorpService corpService;
        private readonly IMapper mapper;
        private readonly ProjectOwnAuthorizer ownAuthorizer;

        public CardReaderDeviceController(CardReaderDeviceService cardReaderDeviceService, ManagementApplicationContext applicationContext, CorpService corpService, IMapper mapper, ProjectOwnAuthorizer ownAuthorizer)
        {
            this.cardReaderDeviceService = cardReaderDeviceService;
            this.applicationContext = applicationContext;
            this.corpService = corpService;
            this.mapper = mapper;
            this.ownAuthorizer = ownAuthorizer;
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("page")]
        public ApiResult<PageDto<CardReaderDeviceDto>> GetDevices([FromQuery] DeviceQuery query, int pageIndex, int pageSize)
        {
            var devices = cardReaderDeviceService.GetSysDevices(query, pageIndex, pageSize);
            List<int> deviceIds = devices.Select(a => a.Id).ToList();
            var dtos = devices.AsPageDto(source => { return source.AsDto(); });
            return ApiResult<PageDto<CardReaderDeviceDto>>.Success(dtos);
        }

        /// <summary>
        /// 创建设备信息
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost("create")]
        [PermissionCheck("sys:cardReaderDevice:add")]
        public ApiResult Add([FromBody] CardReaderDeviceEditForm form)
        {
            CardReader device = new CardReader();
            mapper.Map(form, device);
            device.CreateTime = DateTime.Now;
            device.Status = 设备审核状态.待审核;
            cardReaderDeviceService.CreateSysDevice(device);
            return ApiResult.Success("创建成功");
        }

        /// <summary>
        /// 设备信息表单数据
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <returns></returns>
        [HttpGet("{id}/form")]
        public ApiResult<CardReaderDeviceEditForm> GetRole(int id)
        {
            var device = cardReaderDeviceService.GetSysDevice(id);
            if (device == null)
                return ApiResult<CardReaderDeviceEditForm>.Failure("设备信息数据不存在");
            CardReaderDeviceEditForm deviceEditForm = new CardReaderDeviceEditForm();
            mapper.Map(device, deviceEditForm);
            deviceEditForm.CorpName = corpService.GetCorpInfo(device.CorpCode)?.Name ?? string.Empty;
            return ApiResult<CardReaderDeviceEditForm>.Success(deviceEditForm);
        }

        /// <summary>
        /// 修改设备信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="deviceEditForm"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        [PermissionCheck("sys:cardReaderDevice:edit")]
        public ApiResult Update(int id, [FromBody] CardReaderDeviceEditForm deviceEditForm)
        {
            var device = cardReaderDeviceService.GetSysDevice(id);
            if (device == null) return ApiResult.Failure("设备信息不存在");
            mapper.Map(deviceEditForm, device);
            cardReaderDeviceService.UpdateSysDevice(device);
            return ApiResult.Success("修改成功");
        }

        /// <summary>
        /// 删除设备信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("{ids}")]
        [PermissionCheck("sys:cardReaderDevice:delete")]
        public ApiResult Delete(string ids)
        {
            cardReaderDeviceService.DeleteSysDevices(ValueUtility.ConvertToList<int>(ids));
            return ApiResult.Success("删除成功");
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut("audit/{id}")]
        [PermissionCheck("sys:cardReaderDevice:audit")]
        public ApiResult AuditDevice(int id)
        {
            var device = cardReaderDeviceService.GetSysDevice(id);
            if (device == null) return ApiResult.Failure("设备信息不存在");
            device.AuditUserId = applicationContext.CurrentUser?.Id;
            device.AuditUserName = applicationContext.CurrentUser?.Name ?? string.Empty;
            device.AuditTime = DateTime.Now;
            device.Status = 设备审核状态.已审核;
            cardReaderDeviceService.UpdateSysDevice(device);
            return ApiResult.Success("审核成功");
        }

        /// <summary>
        /// 禁用
        /// </summary>
        /// <param name="id"></param>
        /// <param name="disableReason">禁用原因</param>
        /// <returns></returns>
        [HttpPut("disable/{id}")]
        [PermissionCheck("sys:cardReaderDevice:disable")]
        public ApiResult DisableDevice(int id, string disableReason)
        {
            if (string.IsNullOrWhiteSpace(disableReason)) return ApiResult.Failure("禁用原因不能为空");
            var device = cardReaderDeviceService.GetSysDevice(id);
            if (device == null) return ApiResult.Failure("设备信息不存在");
            device.DisableTime = DateTime.Now;
            device.DisableReason = disableReason;
            device.Status = 设备审核状态.已禁用;
            cardReaderDeviceService.UpdateSysDevice(device);
            return ApiResult.Success("禁用成功");
        }

        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut("enable/{id}")]
        [PermissionCheck("sys:cardReaderDevice:enable")]
        public ApiResult DisableDevice(int id)
        {
            var device = cardReaderDeviceService.GetSysDevice(id);
            if (device == null) return ApiResult.Failure("设备信息不存在");
            device.Status = 设备审核状态.已审核;
            device.DisableTime = null;
            device.DisableReason = "";
            cardReaderDeviceService.UpdateSysDevice(device);
            return ApiResult.Success("启用成功");
        }
    }
}